package huawei.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.res.Resources.Theme;
import android.content.res.TypedArray;
import android.database.DataSetObserver;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.os.Build.VERSION;
import android.util.AttributeSet;
import android.util.Log;
import android.view.ContextThemeWrapper;
import android.view.MotionEvent;
import android.view.PointerIcon;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.ThemedSpinnerAdapter;
import androidx.annotation.ColorInt;
import androidx.annotation.DrawableRes;
import androidx.annotation.Nullable;
import com.huawei.appgallery.festivalanimation.particle.modifiers.SizeModifier;
import huawei.android.widget.DecouplingUtil.ReflectUtil;
import o.bt;
import o.fmz;
import o.fo;
import o.fpt;
import o.fpu;
import o.fqh;

@SuppressLint({"AppCompatCustomView"})
public class HwSpinner extends Spinner implements OnClickListener {
    private int ʻ;
    private fmz ʻॱ;
    private int ʼ;
    private int ʽ;
    private SpinnerAdapter ˊ;
    private float ˊॱ;
    private final Rect ˋ;
    private Context ˋॱ;
    private fpu ˎ;
    private Context ˏ;
    private int ˏॱ;
    private int ͺ;
    OnItemClickListener ॱ;
    private boolean ॱˊ;
    private d ॱॱ;
    private boolean ᐝ;

    static class a implements ListAdapter, SpinnerAdapter {
        private ListAdapter ˋ;
        private SpinnerAdapter ˏ;

        a(@Nullable SpinnerAdapter spinnerAdapter, @Nullable Theme theme) {
            this.ˏ = spinnerAdapter;
            if (spinnerAdapter instanceof ListAdapter) {
                this.ˋ = (ListAdapter) spinnerAdapter;
            }
            if (VERSION.SDK_INT >= 23 && theme != null && (spinnerAdapter instanceof ThemedSpinnerAdapter)) {
                ThemedSpinnerAdapter themedSpinnerAdapter = (ThemedSpinnerAdapter) spinnerAdapter;
                if (themedSpinnerAdapter.getDropDownViewTheme() == null) {
                    themedSpinnerAdapter.setDropDownViewTheme(theme);
                }
            }
        }

        public int getCount() {
            return this.ˏ == null ? 0 : this.ˏ.getCount();
        }

        public Object getItem(int i) {
            return this.ˏ == null ? null : this.ˏ.getItem(i);
        }

        public long getItemId(int i) {
            return this.ˏ == null ? -1 : this.ˏ.getItemId(i);
        }

        public View getView(int i, View view, ViewGroup viewGroup) {
            return getDropDownView(i, view, viewGroup);
        }

        public View getDropDownView(int i, View view, ViewGroup viewGroup) {
            return this.ˏ == null ? null : this.ˏ.getDropDownView(i, view, viewGroup);
        }

        public boolean hasStableIds() {
            return this.ˏ != null && this.ˏ.hasStableIds();
        }

        public void registerDataSetObserver(DataSetObserver dataSetObserver) {
            if (this.ˏ != null) {
                this.ˏ.registerDataSetObserver(dataSetObserver);
            }
        }

        public void unregisterDataSetObserver(DataSetObserver dataSetObserver) {
            if (this.ˏ != null) {
                this.ˏ.unregisterDataSetObserver(dataSetObserver);
            }
        }

        public boolean areAllItemsEnabled() {
            ListAdapter listAdapter = this.ˋ;
            if (listAdapter != null) {
                return listAdapter.areAllItemsEnabled();
            }
            return true;
        }

        public boolean isEnabled(int i) {
            ListAdapter listAdapter = this.ˋ;
            if (listAdapter != null) {
                return listAdapter.isEnabled(i);
            }
            return true;
        }

        public int getItemViewType(int i) {
            return 0;
        }

        public int getViewTypeCount() {
            return 1;
        }

        public boolean isEmpty() {
            return getCount() == 0;
        }
    }

    class b extends fpu {
        final /* synthetic */ HwSpinner ˋ;
        e ॱ;

        b(HwSpinner hwSpinner, View view, e eVar) {
            this.ˋ = hwSpinner;
            super(view);
            this.ॱ = eVar;
        }

        public bt ˏ() {
            return this.ॱ;
        }

        public boolean ˊ() {
            if (!this.ˋ.ॱॱ.ˎ()) {
                this.ˋ.ॱॱ.ॱ(this.ˋ.getTextDirection(), this.ˋ.getTextAlignment());
            }
            return true;
        }
    }

    interface d {
        int ʼ();

        void ˋ(int i);

        CharSequence ˋॱ();

        void ˎ(int i);

        void ˎ(Drawable drawable);

        void ˎ(ListAdapter listAdapter);

        boolean ˎ();

        Drawable ˏ();

        void ˏ(CharSequence charSequence);

        void ॱ(int i, int i2);

        int ॱॱ();
    }

    class e extends HwListPopupWindow implements d {
        private ListAdapter ˊ;
        final /* synthetic */ HwSpinner ˎ;
        private CharSequence ˏ;

        e(final HwSpinner hwSpinner, Context context, AttributeSet attributeSet, int i, int i2) {
            this.ˎ = hwSpinner;
            super(context, attributeSet, i, i2);
            ॱ((View) hwSpinner);
            ˎ(true);
            ˏ(0);
            ˋ(new OnItemClickListener(this) {
                final /* synthetic */ e ॱ;

                public void onItemClick(AdapterView adapterView, View view, int i, long j) {
                    this.ॱ.ˎ.setSelection(i);
                    if (this.ॱ.ˎ.ॱ != null) {
                        this.ॱ.ˎ.performItemClick(view, i, this.ॱ.ˊ.getItemId(i));
                    }
                    this.ॱ.ˋ();
                }
            });
        }

        public void ˎ(ListAdapter listAdapter) {
            super.ˎ(listAdapter);
            this.ˊ = listAdapter;
        }

        public CharSequence ˋॱ() {
            return this.ˏ;
        }

        public void ˏ(CharSequence charSequence) {
            this.ˏ = charSequence;
        }

        int ˊ(int i, int i2, int i3) {
            int i4 = 0;
            if (this.ˊ instanceof SpinnerAdapter) {
                i4 = this.ˎ.ॱ((SpinnerAdapter) this.ˊ, ˏ());
            }
            int i5 = (this.ˎ.ˋॱ.getResources().getDisplayMetrics().widthPixels - this.ˎ.ˋ.left) - this.ˎ.ˋ.right;
            if (i4 > i5) {
                i4 = i5;
            }
            if (i4 >= (i - i2) - i3) {
                return i4;
            }
            return (i - i2) - i3;
        }

        void ॱˊ() {
            int i = 0;
            Drawable ˏ = ˏ();
            if (ˏ != null) {
                ˏ.getPadding(this.ˎ.ˋ);
                i = this.ˎ.ˏ() ? this.ˎ.ˋ.right : -this.ˎ.ˋ.left;
            } else {
                this.ˎ.ˋ.left = 0;
                this.ˎ.ˋ.right = 0;
            }
            int paddingLeft = this.ˎ.getPaddingLeft();
            int paddingRight = this.ˎ.getPaddingRight();
            int width = this.ˎ.getWidth();
            if (this.ˎ.ॱˊ) {
                ˊ(this.ˎ.ॱ(ˊ(width, paddingLeft, paddingRight)));
            } else if (this.ˎ.ʽ == -2) {
                ˊ(ˊ(width, paddingLeft, paddingRight));
            } else if (this.ˎ.ʽ == -1) {
                ˊ((width - paddingLeft) - paddingRight);
            } else {
                ˊ(this.ˎ.ʽ);
            }
            if (this.ˎ.ˏ()) {
                i += (width - paddingRight) - ʻ();
            } else {
                i += paddingLeft;
            }
            ˋ(i);
        }

        public void ॱ(int i, int i2) {
            boolean ˎ = ˎ();
            ॱˊ();
            ʻ(2);
            super.ˊ();
            fpt ˊॱ = ˊॱ();
            ˊॱ.setTint(this.ˎ.ʼ);
            ˊॱ.setChoiceMode(1);
            ˊॱ.setTextDirection(i);
            ˊॱ.setTextAlignment(i2);
            ʼ(this.ˎ.getSelectedItemPosition());
            if (!ˎ) {
            }
        }
    }

    public HwSpinner(Context context) {
        this(context, null);
    }

    public HwSpinner(Context context, AttributeSet attributeSet) {
        this(context, attributeSet, 16842881);
    }

    public HwSpinner(Context context, AttributeSet attributeSet, int i) {
        this(context, attributeSet, i, -1);
    }

    public HwSpinner(Context context, AttributeSet attributeSet, int i, int i2) {
        super(context, attributeSet, i, i2);
        this.ˋ = new Rect();
        this.ᐝ = false;
        this.ॱˊ = false;
        this.ͺ = -1;
        this.ˏॱ = -1;
        this.ˊॱ = SizeModifier.STABLE_STATE_SCALE;
        this.ˋॱ = context;
        ˊ(context, attributeSet, i, i2);
        this.ʻॱ = new fmz(this.ˏ);
        ˏ(context, i);
    }

    private void ˊ(Context context, AttributeSet attributeSet, int i, int i2) {
        TypedArray obtainStyledAttributes = context.obtainStyledAttributes(attributeSet, o.fqk.e.ˉ);
        this.ॱˊ = obtainStyledAttributes.getBoolean(o.fqk.e.ˊˊ, false);
        int resourceId = obtainStyledAttributes.getResourceId(o.fqk.e.ˋᐝ, 0);
        if (resourceId != 0) {
            this.ˏ = new ContextThemeWrapper(context, resourceId);
        } else {
            this.ˏ = context;
        }
        if (VERSION.SDK_INT < 22) {
            setBackgroundResource(o.fqk.d.ˏ);
        } else if (obtainStyledAttributes.hasValueOrEmpty(o.fqk.e.ˊˋ)) {
            setBackground(obtainStyledAttributes.getDrawable(o.fqk.e.ˊˋ));
        }
        switch (i2) {
            case -1:
            case 0:
            case 1:
                e eVar = new e(this, this.ˏ, attributeSet, i, 0);
                TypedArray obtainStyledAttributes2 = this.ˏ.obtainStyledAttributes(attributeSet, o.fqk.e.ˉ, i, 0);
                this.ʽ = obtainStyledAttributes2.getLayoutDimension(o.fqk.e.ˍ, -2);
                ˎ(context, eVar, obtainStyledAttributes2);
                ˏ(eVar, obtainStyledAttributes2);
                eVar.ˎ(obtainStyledAttributes2.getDrawable(o.fqk.e.ˊᐝ));
                eVar.ˏ(obtainStyledAttributes.getString(o.fqk.e.ˋˋ));
                obtainStyledAttributes2.recycle();
                this.ॱॱ = eVar;
                this.ˎ = new b(this, this, eVar);
                break;
        }
        this.ʻ = obtainStyledAttributes.getInt(o.fqk.e.ˌ, 17);
        this.ᐝ = obtainStyledAttributes.getBoolean(o.fqk.e.ˎˎ, false);
        obtainStyledAttributes.recycle();
        if (this.ˊ != null) {
            setAdapter(this.ˊ);
            this.ˊ = null;
        }
    }

    private void ˏ(Context context, int i) {
        if (getBackground() != null) {
            setBackground(new LayerDrawable(new Drawable[]{fqh.ˊ(context, 0), getBackground()}));
            return;
        }
        setBackground(fqh.ˊ(context, i));
    }

    protected void onMeasure(int i, int i2) {
        super.onMeasure(i, i2);
        if (this.ॱॱ != null && MeasureSpec.getMode(i) == Integer.MIN_VALUE) {
            int measuredWidth = getMeasuredWidth();
            int ॱ = ॱ(getAdapter(), getBackground());
            if (measuredWidth < ॱ) {
                measuredWidth = ॱ;
            }
            if (measuredWidth > MeasureSpec.getSize(i)) {
                measuredWidth = MeasureSpec.getSize(i);
            }
            setMeasuredDimension(measuredWidth, getMeasuredHeight());
        }
    }

    int ॱ(SpinnerAdapter spinnerAdapter, Drawable drawable) {
        if (spinnerAdapter == null) {
            return 0;
        }
        int intValue;
        int intValue2;
        Class[] clsArr = new Class[]{Integer.TYPE, Integer.TYPE};
        Object callMethod = ReflectUtil.callMethod(MeasureSpec.class, "makeMeasureSpec", clsArr, new Object[]{Integer.valueOf(getMeasuredWidth()), Integer.valueOf(0)}, MeasureSpec.class);
        if (callMethod != null) {
            intValue = ((Integer) callMethod).intValue();
        } else {
            intValue = 0;
        }
        callMethod = ReflectUtil.callMethod(MeasureSpec.class, "makeMeasureSpec", clsArr, new Object[]{Integer.valueOf(getMeasuredHeight()), Integer.valueOf(0)}, MeasureSpec.class);
        if (callMethod != null) {
            intValue2 = ((Integer) callMethod).intValue();
        } else {
            intValue2 = 0;
        }
        int selectedItemPosition = getSelectedItemPosition() <= 0 ? 0 : getSelectedItemPosition();
        int count = spinnerAdapter.getCount() <= selectedItemPosition + 20 ? spinnerAdapter.getCount() : selectedItemPosition + 20;
        int i = count - selectedItemPosition;
        if (selectedItemPosition - (20 - i) <= 0) {
            selectedItemPosition = 0;
        } else {
            selectedItemPosition -= 20 - i;
        }
        int i2 = selectedItemPosition;
        int i3 = 0;
        View view = null;
        selectedItemPosition = 0;
        while (i2 < count) {
            View view2;
            i = spinnerAdapter.getItemViewType(i2);
            if (i != i3) {
                view2 = null;
            } else {
                i = i3;
                view2 = view;
            }
            view = spinnerAdapter.getView(i2, view2, this);
            if (view == null) {
                return 0;
            }
            if (view.getLayoutParams() == null) {
                view.setLayoutParams(new LayoutParams(-2, -2));
            }
            view.measure(intValue, intValue2);
            if (selectedItemPosition < view.getMeasuredWidth()) {
                selectedItemPosition = view.getMeasuredWidth();
            }
            i2++;
            i3 = i;
        }
        if (drawable != null) {
            drawable.getPadding(this.ˋ);
            selectedItemPosition += this.ˋ.left + this.ˋ.right;
        }
        return selectedItemPosition;
    }

    int ॱ(int i) {
        this.ʻॱ.ॱ(10);
        if (this.ͺ <= 0 || this.ˏॱ <= 0 || this.ˊॱ <= 0.0f) {
            this.ʻॱ.ˋ(this.ˏ);
        } else {
            this.ʻॱ.ˊ(this.ˏ, this.ͺ, this.ˏॱ, this.ˊॱ);
        }
        int ॱ = this.ʻॱ.ॱ();
        int ˎ = this.ʻॱ.ˎ();
        if (i > ॱ) {
            return ॱ;
        }
        if (i < ˎ) {
            return ˎ;
        }
        return i;
    }

    public void setTint(@ColorInt int i) {
        this.ʼ = i;
    }

    public Context getPopupContext() {
        return this.ˏ;
    }

    public void setPopupBackgroundDrawable(Drawable drawable) {
        if (this.ॱॱ instanceof e) {
            this.ॱॱ.ˎ(drawable);
        } else {
            Log.e("HwSpinner", "setPopupBackgroundDrawable: incompatible spinner mode; ignoring...");
        }
    }

    public boolean onTouchEvent(MotionEvent motionEvent) {
        if (this.ˎ == null || !this.ˎ.onTouch(this, motionEvent)) {
            return super.onTouchEvent(motionEvent);
        }
        return true;
    }

    public void setPopupBackgroundResource(@DrawableRes int i) {
        if (VERSION.SDK_INT >= 21) {
            setPopupBackgroundDrawable(getPopupContext().getDrawable(i));
        } else {
            setPopupBackgroundDrawable(fo.ˊ(getPopupContext(), i));
        }
    }

    public Drawable getPopupBackground() {
        return this.ॱॱ.ˏ();
    }

    public void setDropDownVerticalOffset(int i) {
        this.ॱॱ.ˎ(i);
    }

    public int getDropDownVerticalOffset() {
        return this.ॱॱ.ʼ();
    }

    public void setDropDownHorizontalOffset(int i) {
        this.ॱॱ.ˋ(i);
    }

    public int getDropDownHorizontalOffset() {
        return this.ॱॱ.ॱॱ();
    }

    public void setDropDownWidth(int i) {
        if (this.ॱॱ instanceof e) {
            this.ʽ = i;
        } else {
            Log.e("HwSpinner", "Cannot set dropdown width for MODE_DIALOG, ignoring");
        }
    }

    public int getDropDownWidth() {
        return this.ʽ;
    }

    public void setEnabled(boolean z) {
        super.setEnabled(z);
        if (this.ᐝ) {
            int childCount = getChildCount();
            for (int i = 0; i < childCount; i++) {
                getChildAt(i).setEnabled(z);
            }
        }
    }

    public void setGravity(int i) {
        if (this.ʻ != i) {
            if ((i & 7) == 0) {
                i |= 8388611;
            }
            this.ʻ = i;
            requestLayout();
        }
    }

    public int getGravity() {
        return this.ʻ;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        throw new RuntimeException("setOnItemClickListener cannot be used with a spinner.");
    }

    public void setOnItemClickListenerInt(OnItemClickListener onItemClickListener) {
        super.setOnItemClickListener(onItemClickListener);
    }

    public boolean performClick() {
        if (getAdapter() == null || getAdapter().getCount() < 1) {
            return false;
        }
        if (this.ॱॱ.ˎ()) {
            return true;
        }
        this.ॱॱ.ॱ(getTextDirection(), getTextAlignment());
        return true;
    }

    public void onClick(DialogInterface dialogInterface, int i) {
        setSelection(i);
        dialogInterface.dismiss();
    }

    public CharSequence getAccessibilityClassName() {
        return HwSpinner.class.getName();
    }

    public void setPrompt(CharSequence charSequence) {
        this.ॱॱ.ˏ(charSequence);
    }

    public void setPromptId(int i) {
        setPrompt(getContext().getText(i));
    }

    public CharSequence getPrompt() {
        return this.ॱॱ.ˋॱ();
    }

    public PointerIcon onResolvePointerIcon(MotionEvent motionEvent, int i) {
        if (VERSION.SDK_INT >= 24) {
            Object obj = (getPointerIcon() == null && isClickable() && isEnabled()) ? 1 : null;
            if (obj != null) {
                return PointerIcon.getSystemIcon(getContext(), 1002);
            }
        }
        return super.onResolvePointerIcon(motionEvent, i);
    }

    public void setAdapter(SpinnerAdapter spinnerAdapter) {
        if (this.ॱॱ == null) {
            this.ˊ = spinnerAdapter;
            return;
        }
        super.setAdapter(spinnerAdapter);
        if (this.ˋॱ.getApplicationInfo().targetSdkVersion < 21 || spinnerAdapter == null || spinnerAdapter.getViewTypeCount() == 1) {
            this.ॱॱ.ˎ(new a(spinnerAdapter, (this.ˏ == null ? this.ˋॱ : this.ˏ).getTheme()));
            return;
        }
        throw new IllegalArgumentException("Spinner adapter view type count must be 1");
    }

    private boolean ˏ() {
        return getLayoutDirection() == 1;
    }

    public void setColumnEnadled(boolean z) {
        this.ॱˊ = z;
        requestLayout();
    }

    private void ˎ(Context context, e eVar, TypedArray typedArray) {
        if (VERSION.SDK_INT < 22) {
            eVar.ˊ(fo.ˊ(context, o.fqk.d.ʼ));
        } else if (typedArray.hasValueOrEmpty(o.fqk.e.ˋˊ)) {
            eVar.ˊ(typedArray.getDrawable(o.fqk.e.ˋˊ));
        }
    }

    private void ˏ(e eVar, TypedArray typedArray) {
        Drawable drawable = typedArray.getDrawable(o.fqk.e.ˏˏ);
        if (drawable == null) {
            drawable = fo.ˊ(this.ˋॱ, o.fqk.d.ˎ);
        }
        eVar.ˊ(drawable, 0);
        drawable = typedArray.getDrawable(o.fqk.e.ˑ);
        if (drawable == null) {
            drawable = fo.ˊ(this.ˋॱ, o.fqk.d.ॱॱ);
        }
        eVar.ˊ(drawable, 1);
        drawable = typedArray.getDrawable(o.fqk.e.ˏˎ);
        if (drawable == null) {
            drawable = fo.ˊ(this.ˋॱ, o.fqk.d.ˊ);
        }
        eVar.ˊ(drawable, 2);
        drawable = typedArray.getDrawable(o.fqk.e.ˎˏ);
        if (drawable == null) {
            drawable = fo.ˊ(this.ˋॱ, o.fqk.d.ˋ);
        }
        eVar.ˊ(drawable, 3);
    }
}
